var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//前端
const userAuth = require('./middlewares/user-auth');//中间件

var indexRouter = require('./routes/index');
const coursesRouter = require('./routes/courses');
const categoriesRouter = require('./routes/categories');
const chaptersRouter = require('./routes/chapters');
const articlesRouter = require('./routes/articles');
const settingsRouter = require('./routes/settings');
const searchRouter = require('./routes/search');
const authRouter = require('./routes/auth');
const usersRouter = require('./routes/users')


// 后台路由文件
const adminAuth = require('./middlewares/admin-auth');//中间件
const adminAuthRouter = require('./routes/admin/auth');//中间件-注入
const adminArticlesRouter = require('./routes/admin/articles');
const adminCategoriesRouter = require('./routes/admin/categories');
const adminSettingsRouter = require('./routes/admin/settings');
const adminUsersRouter = require('./routes/admin/users');
const adminCoursesRouter = require('./routes/admin/courses');
const adminChapterRouter = require('./routes/admin/chapters');
const adminChartsRouter = require('./routes/admin/charts');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//记得删除plublic/index.html,否则影响访问路径'/'
app.use(express.static(path.join(__dirname, 'public')));
//前端
app.use('/', indexRouter);
app.use('/categories', categoriesRouter);
app.use('/courses', coursesRouter);
app.use('/chapters', chaptersRouter);
app.use('/articles', articlesRouter);
app.use('/settings', settingsRouter);
app.use('/search', searchRouter);
app.use('/auth', authRouter);
app.use('/users', userAuth, usersRouter);

//后端
app.use('/admin/articles', adminAuth, adminArticlesRouter);
app.use('/admin/categories', adminAuth, adminCategoriesRouter);
app.use('/admin/settings', adminAuth, adminSettingsRouter);
app.use('/admin/users', adminAuth, adminUsersRouter);
app.use('/admin/courses', adminAuth, adminCoursesRouter);
app.use('/admin/chapters', adminAuth, adminChapterRouter);
app.use('/admin/charts', adminAuth, adminChartsRouter);
//这个不要做登陆校验，因为它未登陆
app.use('/admin/auth', adminAuthRouter);
module.exports = app;
